let ImportService = require('./ImportService')
let Mongodb = require('../../database/mongodb')

class TestCaseService extends ImportService{
  initParam () {
    this.tableConfigs = [
      { tableName: 'users' },
      { tableName: 'purchasecontract', fields: ['purchaseno'] }
    ]
    this.enumTypes = ['Contract_type']
  }

  formateData (dataList) {
    return new Promise((resolve, reject) => {
      let rowDatas = []
      let data = {}
      for (let i = 0, len = dataList.length; i < len; i++) {
        data = dataList[i]
        if (this.checkReapt(data[1])) {
          rowDatas.push({
            "purchaseno": data[1],
            "company": data[2],
            "operator": this.dataFormater('users', data[3], 'email', 'userName'),
            "itemName": data[4],
            "payno": data[5],
            "orderno": data[6],
            "money": data[7],
            "contractDate": data[8],
            "type": this.dataFormater('Contract_type', data[9]),
            "note": data[10],
            "orgId": this.user.orgId[0],
            "createDate": new Date(),
            "createUser": this.user.email
          })
        } else {
          console.log('数据重复')
        }
      }
      resolve(rowDatas)
    })
  }

  checkReapt (purchaseno) {
    if (this.dataCache['purchasecontract'] && this.dataCache['purchasecontract'].length > 0) {      
      let data = this.dataCache['purchasecontract'].find(item => {        
        return item['purchaseno'] === purchaseno
      })
      if (data) {
        return false
      } else {
        return true
      }
    } else {
      return true
    }
  }
}

module.exports = new TestCaseService()